/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package advsm;

import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JSpinner;
import javax.swing.SpinnerDateModel;
import javax.swing.SpinnerModel;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import logic.ControladorVentas;
import logic.FacturaInterface;
import logic.ModeloTablaVentas;
import logic.ValeInterface;
import logic.VentaArrayObserver;
import logic.VentaInterface;
import logic.pago.PagoConstants;

/**
 *
 * @author Dani
 */
public class MainWindow extends JFrame implements VentaArrayObserver{

    /**
     * Creates new form MainWindow
     */
    public MainWindow(ControladorVentas contr) {
        super("Advanced Shop Manager");
        this.contr = contr;
        initComponents();
        configureSpinners();
        configureExitButton();
        tableVentas.setModel(modelVentas);
        
        TextFieldSearchObserver obs = new TextFieldSearchObserver();
        textFieldSearchVendedor.getDocument().addDocumentListener(obs);
        textFieldSearchCliente.getDocument().addDocumentListener(obs);
        textFieldSearchPrenda.getDocument().addDocumentListener(obs);
        checkBoxSearchFecha.addItemListener(new CheckBoxSearchObserver());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelSearch = new javax.swing.JPanel();
        labelSearchVendedor = new javax.swing.JLabel();
        labelSearchCliente = new javax.swing.JLabel();
        labelSearchPrenda = new javax.swing.JLabel();
        textFieldSearchVendedor = new javax.swing.JTextField();
        textFieldSearchCliente = new javax.swing.JTextField();
        textFieldSearchPrenda = new javax.swing.JTextField();
        checkBoxSearchFecha = new javax.swing.JCheckBox();
        javax.swing.SpinnerModel model2 = new SpinnerDateModel();
        spinnerSearchFecha = new javax.swing.JSpinner(model2);
        panelVenta = new javax.swing.JPanel();
        labelVendedor2 = new javax.swing.JLabel();
        labelCliente2 = new javax.swing.JLabel();
        labelFecha2 = new javax.swing.JLabel();
        textFieldVendedor2 = new javax.swing.JTextField();
        textFieldCliente2 = new javax.swing.JTextField();
        checkBoxComentarios = new javax.swing.JCheckBox();
        Calendar cal = Calendar.getInstance();
        Date now = cal.getTime();
        cal.add(Calendar.YEAR, -50);
        Date startDate = cal.getTime();
        cal.add(Calendar.YEAR, 100);
        Date endDate = cal.getTime();
        SpinnerModel model =
        new SpinnerDateModel(now, startDate, endDate, Calendar.YEAR);
        javax.swing.SpinnerModel model1 = new SpinnerDateModel(now, startDate, endDate,
            Calendar.DAY_OF_WEEK);
        spinnerFecha2 = new javax.swing.JSpinner(model1);
        labelFactura2 = new javax.swing.JLabel();
        textFieldFactura2 = new javax.swing.JTextField();
        labelComentarios2 = new javax.swing.JLabel();
        scrollPaneComentarios = new javax.swing.JScrollPane();
        textPaneComentarios = new javax.swing.JTextPane();
        togglePagoTarjeta = new javax.swing.JToggleButton();
        textFieldTarjeta = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        panelVentas = new javax.swing.JPanel();
        scrollPaneTablaVentas = new javax.swing.JScrollPane();
        tableVentas = new javax.swing.JTable();
        labelPrenda2 = new javax.swing.JLabel();
        textFieldPrenda2 = new javax.swing.JTextField();
        labelPrenda3 = new javax.swing.JLabel();
        textFieldVale = new javax.swing.JTextField();
        buttonDevolucion = new javax.swing.JButton();
        labelImporte = new javax.swing.JLabel();
        textFieldImporte = new javax.swing.JTextField();
        labelVale = new javax.swing.JLabel();
        textFieldValeAutogenerado = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        textAreaFactura = new javax.swing.JTextArea();
        buttonRealizaVenta = new javax.swing.JButton();
        buttonImprimirFactura = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setIconImage(getIconImage());

        panelSearch.setBorder(javax.swing.BorderFactory.createTitledBorder("Búsqueda en historial"));

        labelSearchVendedor.setText("Vendedor");

        labelSearchCliente.setText("Cliente");

        labelSearchPrenda.setText("Prenda");

        checkBoxSearchFecha.setText("Fecha");

        javax.swing.GroupLayout panelSearchLayout = new javax.swing.GroupLayout(panelSearch);
        panelSearch.setLayout(panelSearchLayout);
        panelSearchLayout.setHorizontalGroup(
            panelSearchLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelSearchLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelSearchLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelSearchLayout.createSequentialGroup()
                        .addGroup(panelSearchLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(labelSearchPrenda)
                            .addComponent(checkBoxSearchFecha))
                        .addGroup(panelSearchLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(panelSearchLayout.createSequentialGroup()
                                .addGap(47, 47, 47)
                                .addComponent(textFieldSearchPrenda, javax.swing.GroupLayout.DEFAULT_SIZE, 215, Short.MAX_VALUE))
                            .addGroup(panelSearchLayout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(spinnerSearchFecha, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addContainerGap())
                    .addGroup(panelSearchLayout.createSequentialGroup()
                        .addGroup(panelSearchLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(labelSearchCliente)
                            .addComponent(labelSearchVendedor))
                        .addGap(56, 56, 56)
                        .addGroup(panelSearchLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(textFieldSearchVendedor)
                            .addComponent(textFieldSearchCliente))
                        .addGap(10, 10, 10))))
        );
        panelSearchLayout.setVerticalGroup(
            panelSearchLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelSearchLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(panelSearchLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelSearchVendedor)
                    .addComponent(textFieldSearchVendedor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelSearchLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelSearchCliente)
                    .addComponent(textFieldSearchCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelSearchLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelSearchPrenda)
                    .addComponent(textFieldSearchPrenda, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelSearchLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(checkBoxSearchFecha)
                    .addComponent(spinnerSearchFecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(26, 26, 26))
        );

        panelVenta.setBorder(javax.swing.BorderFactory.createTitledBorder("Realizar nueva venta"));

        labelVendedor2.setText("Vendedor");

        labelCliente2.setText("Cliente");

        labelFecha2.setText("Fecha");

        labelFactura2.setText("Número de factura");

        textFieldFactura2.setText("0001");

        labelComentarios2.setText("Comentarios:");

        scrollPaneComentarios.setViewportView(textPaneComentarios);

        togglePagoTarjeta.setText("Pagar con tarjeta");

        jLabel5.setText("Número");

        javax.swing.GroupLayout panelVentaLayout = new javax.swing.GroupLayout(panelVenta);
        panelVenta.setLayout(panelVentaLayout);
        panelVentaLayout.setHorizontalGroup(
            panelVentaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelVentaLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelVentaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelVentaLayout.createSequentialGroup()
                        .addGroup(panelVentaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(labelCliente2, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(labelVendedor2, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(panelVentaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(textFieldCliente2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 228, Short.MAX_VALUE)
                            .addComponent(textFieldVendedor2, javax.swing.GroupLayout.Alignment.TRAILING)))
                    .addGroup(panelVentaLayout.createSequentialGroup()
                        .addGroup(panelVentaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(labelFactura2)
                            .addComponent(labelFecha2, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(panelVentaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(panelVentaLayout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(textFieldFactura2))
                            .addGroup(panelVentaLayout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(spinnerFecha2, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addComponent(scrollPaneComentarios)
                    .addGroup(panelVentaLayout.createSequentialGroup()
                        .addComponent(labelComentarios2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(checkBoxComentarios)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(panelVentaLayout.createSequentialGroup()
                        .addComponent(togglePagoTarjeta)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel5)
                        .addGap(18, 18, 18)
                        .addComponent(textFieldTarjeta, javax.swing.GroupLayout.DEFAULT_SIZE, 127, Short.MAX_VALUE)))
                .addContainerGap())
        );
        panelVentaLayout.setVerticalGroup(
            panelVentaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelVentaLayout.createSequentialGroup()
                .addGroup(panelVentaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelVendedor2)
                    .addComponent(textFieldVendedor2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelVentaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelCliente2, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(textFieldCliente2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelVentaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelFecha2)
                    .addComponent(spinnerFecha2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelVentaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelFactura2)
                    .addComponent(textFieldFactura2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(9, 9, 9)
                .addGroup(panelVentaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelComentarios2)
                    .addComponent(checkBoxComentarios))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(scrollPaneComentarios, javax.swing.GroupLayout.DEFAULT_SIZE, 94, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(panelVentaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(togglePagoTarjeta)
                    .addComponent(textFieldTarjeta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addContainerGap())
        );

        tableVentas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Fecha", "Cliente", "Empleado", "Precio", "Descuento"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Double.class, java.lang.Integer.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        scrollPaneTablaVentas.setViewportView(tableVentas);

        labelPrenda2.setText("Prenda (Separadas por ;)");

        labelPrenda3.setText("Vale (Separados por ;)");

        buttonDevolucion.setText("Realizar Devolución");
        buttonDevolucion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonDevolucionActionPerformed(evt);
            }
        });

        labelImporte.setText("Importe");

        labelVale.setText("Vale Autogenerado");

        textFieldValeAutogenerado.setEditable(false);

        textAreaFactura.setEditable(false);
        textAreaFactura.setColumns(20);
        textAreaFactura.setRows(5);
        jScrollPane1.setViewportView(textAreaFactura);

        javax.swing.GroupLayout panelVentasLayout = new javax.swing.GroupLayout(panelVentas);
        panelVentas.setLayout(panelVentasLayout);
        panelVentasLayout.setHorizontalGroup(
            panelVentasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelVentasLayout.createSequentialGroup()
                .addGroup(panelVentasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(scrollPaneTablaVentas, javax.swing.GroupLayout.DEFAULT_SIZE, 468, Short.MAX_VALUE)
                    .addGroup(panelVentasLayout.createSequentialGroup()
                        .addGroup(panelVentasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(panelVentasLayout.createSequentialGroup()
                                .addComponent(buttonDevolucion)
                                .addGap(10, 10, 10)
                                .addComponent(labelImporte)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(textFieldImporte, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(panelVentasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(textFieldVale)
                                .addComponent(labelPrenda3, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(textFieldPrenda2, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(labelPrenda2))
                        .addGap(18, 18, 18)
                        .addGroup(panelVentasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1)
                            .addGroup(panelVentasLayout.createSequentialGroup()
                                .addComponent(labelVale)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(textFieldValeAutogenerado)))))
                .addContainerGap())
        );
        panelVentasLayout.setVerticalGroup(
            panelVentasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelVentasLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(scrollPaneTablaVentas, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelVentasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonDevolucion)
                    .addComponent(labelImporte)
                    .addComponent(textFieldImporte, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelVale)
                    .addComponent(textFieldValeAutogenerado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelVentasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelVentasLayout.createSequentialGroup()
                        .addComponent(labelPrenda2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textFieldPrenda2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(16, 16, 16)
                        .addComponent(labelPrenda3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textFieldVale, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        buttonRealizaVenta.setText("Realizar Venta");
        buttonRealizaVenta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonRealizaVentaActionPerformed(evt);
            }
        });

        buttonImprimirFactura.setText("Imprimir Factura");
        buttonImprimirFactura.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonImprimirFacturaActionPerformed(evt);
            }
        });

        jLabel1.setText("ADVSM Advanced Shop Manager ©2013");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(buttonRealizaVenta, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(panelVenta, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(panelSearch, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(panelVentas, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(buttonImprimirFactura, javax.swing.GroupLayout.PREFERRED_SIZE, 231, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(panelSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(panelVenta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(panelVentas, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(buttonImprimirFactura)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(buttonRealizaVenta)
                        .addComponent(jLabel1)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    @Override
    public void inventoryChange(List<VentaInterface> inventory) {
        if (modelVentas.getRowCount() != 0)
            modelVentas.removeRowRange(0, tableVentas.getRowCount()-1);
        modelVentas.insertRows(0, inventory);
    }

    private void configureSpinners (){
        Calendar cal = Calendar.getInstance();
        Date now = cal.getTime();
        cal.add(Calendar.YEAR, -20); 
        Date startDate = cal.getTime();
        spinnerFecha2.setModel(new SpinnerDateModel(now, startDate, now, Calendar.DAY_OF_WEEK));
        spinnerSearchFecha.setModel(new SpinnerDateModel(now, startDate, now, Calendar.DAY_OF_WEEK));
        spinnerFecha2.setEditor (new JSpinner.DateEditor(spinnerFecha2, "dd-MM-yyyy"));
        spinnerSearchFecha.setEditor(new JSpinner.DateEditor(spinnerSearchFecha, "dd-MM-yyyy"));
    }
    
    private void configureExitButton (){
        final JFrame frame = this;
                frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

        this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                contr.requestSaveAndQuit();
            }
        });
    }
    
    @Override
    public Image getIconImage() {

        return Toolkit.getDefaultToolkit().
                getImage(ClassLoader.getSystemResource("icons/mainIcon.png"));
    }
    
    private void buttonRealizaVentaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonRealizaVentaActionPerformed
        //List<Prenda> listaPrendas = modelPrendas.getRowsAsList();
        //List<Vale> listaVales = modelVales.getRowsAsList();
        if (textFieldPrenda2.getText().isEmpty() || textFieldCliente2.getText().isEmpty() ||
                textFieldVendedor2.getText().isEmpty() || 
                togglePagoTarjeta.isSelected() && textFieldTarjeta.getText().isEmpty()){
            JOptionPane.showMessageDialog(this, "Error. Faltan datos para realizar la venta.");
            return;
        }
        
        Calendar c = Calendar.getInstance(); 
        c.setTime(((SpinnerDateModel)spinnerFecha2.getModel()).getDate());
        String p;
        if (togglePagoTarjeta.isSelected())
            p = PagoConstants.PAGO_TARJETA.toString() + ";" + textFieldTarjeta.getText();
        else
            p = PagoConstants.PAGO_EFECTIVO.toString();
        String comentarios = null;
        if (checkBoxComentarios.isSelected() && !textPaneComentarios.getText().isEmpty()) 
            comentarios = textPaneComentarios.getText();
        int id = Integer.parseInt(textFieldFactura2.getText());
        
        this.contr.createAndAddVenta(textFieldPrenda2.getText(), textFieldCliente2.getText(), 
                                    textFieldVendedor2.getText(), c,
                                    comentarios, textFieldVale.getText(), p, id);
    }//GEN-LAST:event_buttonRealizaVentaActionPerformed

    private void buttonDevolucionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonDevolucionActionPerformed
       if (textFieldImporte.getText().isEmpty())
            return;
       int row = tableVentas.getSelectedRow();
       if (row != -1)
           this.contr.descuenta(Integer.parseInt(textFieldImporte.getText()), 
                            modelVentas.getRow(row).getId());
       
    }//GEN-LAST:event_buttonDevolucionActionPerformed

    private void buttonImprimirFacturaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonImprimirFacturaActionPerformed
       int row = tableVentas.getSelectedRow();
       if (row != -1)
           this.contr.factura(modelVentas.getRow(row).getId());
       else
           JOptionPane.showMessageDialog(this, "No se ha seleccionado venta");
    }//GEN-LAST:event_buttonImprimirFacturaActionPerformed

  
        
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonDevolucion;
    private javax.swing.JButton buttonImprimirFactura;
    private javax.swing.JButton buttonRealizaVenta;
    private javax.swing.JCheckBox checkBoxComentarios;
    private javax.swing.JCheckBox checkBoxSearchFecha;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel labelCliente2;
    private javax.swing.JLabel labelComentarios2;
    private javax.swing.JLabel labelFactura2;
    private javax.swing.JLabel labelFecha2;
    private javax.swing.JLabel labelImporte;
    private javax.swing.JLabel labelPrenda2;
    private javax.swing.JLabel labelPrenda3;
    private javax.swing.JLabel labelSearchCliente;
    private javax.swing.JLabel labelSearchPrenda;
    private javax.swing.JLabel labelSearchVendedor;
    private javax.swing.JLabel labelVale;
    private javax.swing.JLabel labelVendedor2;
    private javax.swing.JPanel panelSearch;
    private javax.swing.JPanel panelVenta;
    private javax.swing.JPanel panelVentas;
    private javax.swing.JScrollPane scrollPaneComentarios;
    private javax.swing.JScrollPane scrollPaneTablaVentas;
    private javax.swing.JSpinner spinnerFecha2;
    private javax.swing.JSpinner spinnerSearchFecha;
    private javax.swing.JTable tableVentas;
    private javax.swing.JTextArea textAreaFactura;
    private javax.swing.JTextField textFieldCliente2;
    private javax.swing.JTextField textFieldFactura2;
    private javax.swing.JTextField textFieldImporte;
    private javax.swing.JTextField textFieldPrenda2;
    private javax.swing.JTextField textFieldSearchCliente;
    private javax.swing.JTextField textFieldSearchPrenda;
    private javax.swing.JTextField textFieldSearchVendedor;
    private javax.swing.JTextField textFieldTarjeta;
    private javax.swing.JTextField textFieldVale;
    private javax.swing.JTextField textFieldValeAutogenerado;
    private javax.swing.JTextField textFieldVendedor2;
    private javax.swing.JTextPane textPaneComentarios;
    private javax.swing.JToggleButton togglePagoTarjeta;
    // End of variables declaration//GEN-END:variables
 
    private ControladorVentas contr;
    private ModeloTablaVentas modelVentas = new ModeloTablaVentas();

    @Override
    public void newValeGenerado(ValeInterface v) {
        this.textFieldValeAutogenerado.setText(""+v.getIdentificador());
    }

    @Override
    public void quitAvailable() {
        if (JOptionPane.showConfirmDialog(this, "¿Desea salir?") == JOptionPane.YES_OPTION)
            this.dispose();
        
    }

    private void sendSearchRequest() {
        this.contr.performSearch(
            textFieldSearchCliente.getText(),
            textFieldSearchPrenda.getText(), 
            textFieldSearchVendedor.getText(),
            checkBoxSearchFecha.isSelected() ? 
            ((SpinnerDateModel)spinnerSearchFecha.getModel()).getDate(): null);
    }

    @Override
    public void newFacturaGenerada(FacturaInterface f) {
        textAreaFactura.setText(f.toString());
    }

    @Override
    public void errorOccured(String s) {
        JOptionPane.showMessageDialog(this, s);
    }
    
    public class TextFieldSearchObserver implements DocumentListener {

        @Override
        public void insertUpdate(DocumentEvent e) {
            sendSearchRequest();
        }

        @Override
        public void removeUpdate(DocumentEvent e) {
            sendSearchRequest();
        }

        @Override
        public void changedUpdate(DocumentEvent e) {
        }
        
    }
    
    public class CheckBoxSearchObserver implements ItemListener{

        @Override
        public void itemStateChanged(ItemEvent e) {
            sendSearchRequest();
        }
        
    }
}